package a3_p04_ak_fh;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jgrapht.WeightedGraph;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.DirectedWeightedMultigraph;
import org.jgrapht.graph.WeightedMultigraph;

import a2_p04_ak_fh.AttVertex;

public class RandomGraph {

	

	public static WeightedGraph<AttVertex, DefaultWeightedEdge> getRandomGraph(
			int vertices, int edges) {
		
		long time = System.currentTimeMillis();
		
		final int MAX_WEIGHT = (int) (edges+edges*0.5);

		WeightedGraph<AttVertex, DefaultWeightedEdge> graph = new WeightedMultigraph<AttVertex, DefaultWeightedEdge>(
				DefaultWeightedEdge.class);

		List<AttVertex> verticeList = new ArrayList<AttVertex>();
		Set<Integer> usedWeights = new HashSet<Integer>();

		for (int i = 0; i < vertices; i++) {
			AttVertex v = new AttVertex("v" + i, 0.0);
			graph.addVertex(v);
			verticeList.add(v);
		}

		
		for (int i = 0; i <= edges; i++) {
			DefaultWeightedEdge edge = new DefaultWeightedEdge();
			
			int e1 = myRandom(vertices - 1);
			int e2 = myRandom(vertices - 1);
			
			while(e1 == e2) {
				e2 = myRandom(vertices - 1);
			}
			edge = graph.addEdge(verticeList.get(e1),
					verticeList.get(e2));

			int newWeight = myRandom(MAX_WEIGHT);
			
			while(usedWeights.contains(newWeight)) {
				newWeight = myRandom(MAX_WEIGHT);
			}
			
			usedWeights.add(newWeight);
			
			graph.setEdgeWeight(edge, newWeight);
			
		}
		
		time=System.currentTimeMillis()-time;
		System.out.println("Graph generiert in " + time + "ms");

		return graph;
	}

	public static int myRandom(int high) {
		return (int) (Math.random() * (high -0) + 0);
	}
}
